import {
  postForm
}
from '../../utils/request'
Page({

  data: {
    basePath: getApp().globalData.basePath,
    showEditNameDialog: false, // 显示修改昵称对话框
  },
  onLoad(options) {
    let user = wx.getStorageSync('user');
    // delete user.mobile
    this.setData({
      user: user
    })
  },
  onReady() {},
  onShow() {},
  onHide() {},
  onUnload() {},
  onPullDownRefresh() {},
  onReachBottom() {},
  onShareAppMessage() {},
  getPhoneNumber(e) {
    if (!e.detail.code) {
      wx.showToast({
        title: '无法获取手机号',
        icon: 'none'
      })
      return;
    }
    let code = e.detail.code;
    postForm({
      url: 'front/api/user/mobile',
      data: {
        code: code
      }
    }).then(resp => {
      let mobile = resp;
      let user = this.data.user;
      user.mobile = mobile;
      this.setData({
        user: user
      });
      wx.setStorageSync('mobile', mobile);
      wx.setStorageSync('user', user);
    })
  },
  clickShowEditNameDialog() {
    this.setData({
      showEditNameDialog: true
    })
  },
  closeEditNameDialog() {
    this.setData({
      showEditNameDialog: false
    })
  },
  saveEditNickname(e) {
    let name = e.detail.value.nickname;
    if (!name) {
      return;
    }
    postForm({
      url: 'front/api/user/modify/nickname',
      data: {
        nickName: name
      }
    }).then(resp => {
      let user = resp;
      this.setData({
        user: user,
        showEditNameDialog: false
      })
      wx.setStorageSync('user', user);
    })
  },
  getProfile(e) {
    let filePath = e.detail.avatarUrl;
    let that = this;
    wx.uploadFile({
      filePath: filePath,
      name: 'file',
      url: this.data.basePath + '/front/api/files/upload/image',
      header: {
        uid: wx.getStorageSync("userId") | 0
      },
      success(resp) {
        let response = JSON.parse(resp.data);
        let url = response.data;

        postForm({
          url: 'front/api/user/modify/profile',
          data: {
            profile: url
          }
        }).then(resp => {
          let user = resp;
          that.setData({
            user: user
          })
          wx.setStorageSync('user', user);
        })
      }
    })
  }
})